package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 * @author Lucie
 *class Update_Book_Details_controler
 *Class variables:msg,flag
 *Class methods:get_item,get_all_authors,check_input,get_all_subjects,get_sub_of_item,get_author_of_item,get_msg,get_sub_of_item,set_msg
 */
public class Update_Book_Details_controler {
	private String msg; 
	private boolean flag;
	
	/**
	 * Constructor initializes the class variables
	 */
	public Update_Book_Details_controler ()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	  *  @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	
/**
 * * this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 * then we call getItemByID that return arraylist with all the details of the item if the item not exist
	 * its return null
 * @param id
 * @return ArrayList<String> with all the information of the item chosen if there is not such an item its return null
 */
	public ArrayList<String> get_item(String id)
	{
		ArrayList<String> str_item=new  ArrayList<String>(); 
		 check_input(id);
		 if (flag==false)
			 return null;
		 str_item=BusinessImplements.GetInstance().getServerProxy().getItemByID(id);
		 if (str_item.get(0)==null)
		 {
			 set_msg("\nitem number not exist "); 
			 return null;
		 }
		return str_item;
	}
	
	
	/**
	 * this  method call the class ;serverproxy, to Contact with the server,
	 * we call the method getAuthors() that return from the db all the authors that exist in the db
	 * @return ArrayList<String> with all the authors names and id
	 * @return
	 */
	 
	public   ArrayList<String> get_all_authors()
	{
		 ArrayList<String> str_authors=new  ArrayList<String>(); 

		str_authors=BusinessImplements.GetInstance().getServerProxy().getAuthors();
		
		
		return str_authors;

	}
		/**
		 * this  method call the class ;serverproxy, to Contact with the server,
		 * we call the method getSubjects() that return from the db all the subjects that exist in the db
		 *  
		 * @return ArrayList<String> with all the subjects names and id
		
		 */
	public ArrayList<String> get_all_subjects()
	{
		 ArrayList<String> str_subjects=new  ArrayList<String>();
		
		 str_subjects=BusinessImplements.GetInstance().getServerProxy().getSubjects();
	
		return str_subjects;
		
	}
	
	/**
		 * this  method call the class ;serverproxy, to Contact with the server,
		 * we call the method get_SubjectsByItemId that return from the db all the subjects that relate to a chosen item 
		 * @return ArrayList<String> with all the subjects id that relate to the item 
	 */
	public ArrayList<String> get_sub_of_item(String id)
	{
		 ArrayList<String> str_subjects=new  ArrayList<String>();
		
		 str_subjects=BusinessImplements.GetInstance().getServerProxy().get_SubjectsByItemId(id);
		 
		return str_subjects;
		
	}
/**
	 * this  method call the class ;serverproxy, to Contact with the server,
		 * we call the method get_AuthorsByItemId that return from the db all the authors that relate to a chosen item 
		 * @return ArrayList<String> with all the authors id that relate to the item 
 */
	public ArrayList<String> get_author_of_item(String id)
	{
		 ArrayList<String> str_authors=new  ArrayList<String>(); 

			str_authors=BusinessImplements.GetInstance().getServerProxy().get_AuthorsByItemId(id);
			
		return str_authors;
			
	}
	
	
	/**
	 * 
	  * this  method call the class ;serverproxy, to Contact with the server,
	  * the main purpose of this method is to add a new subject to a chosen item 
		 * we call the method check_addItemSubject that return false if the item already have the item we are trying to add 
		 * if thats the case we return false if everything is ok we call the method "addItemSubject" that add the subject to the item
		 * if something went wrong we return false
		 * @return return true if everything as ok else return false
	 */
	
	public boolean addSubjectToItem(String copy_id,String sub_id)
	{
		 ArrayList<String> str_sub=new  ArrayList<String>(); 
		 ArrayList<String> str_check=new  ArrayList<String>();
		 	String str[]=new String[1];
		 	str[0]=sub_id;
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_addItemSubject(copy_id, sub_id);
		 	if (str_check.get(0).equals("false"))
			{
				set_msg("\ncoud not add subject to item\n subject all ready exist in item subject list "); 
				 return false;
			}
			str_sub=BusinessImplements.GetInstance().getServerProxy().addItemSubject(copy_id, str);
			if (str_sub.get(0).equals("false"))
			{
				set_msg("\ncoud not add subject to item\nplease try again "); 
				 return false;
			}
		return true;
			
	}
	/**
	 * 
	 * @param copy_id
	 * @param author_id
	 * this  method call the class ;serverproxy, to Contact with the server,
	  * the main purpose of this method is to add a new author to a chosen item
		 * we call the method check_addItemAuthor that return false if the item already have the item we are trying to add 
		 * if thats the case we return false if everything is ok we call the method "addItemAuthor" that add the author to the item
		 * if something went wrong we return false
		 * @return return true if everything as ok else return false
	 */
	public boolean addAuthorToItem(String copy_id,String author_id)
	{
		 ArrayList<String> str_authors=new  ArrayList<String>(); 
		 ArrayList<String> str_check=new  ArrayList<String>();
		 String str[]=new String[1];
		 	str[0]=author_id;
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_addItemAuthor(copy_id, author_id);
		 	if (str_check.get(0).equals("false"))
			{
				set_msg("\ncoud not add author to item\n author all ready exist in item authors list "); 
				 return false;
			}
			str_authors=BusinessImplements.GetInstance().getServerProxy().addItemAuthor(copy_id, str);
			if (str_authors.get(0).equals("false"))
			{
				set_msg("\ncoud not add author to item\nplease try again "); 
				 return false;
			}
		return true;
			
	}
	
	/**
	 * 
	 * @param id
	 * @param name
	 * @param edition
	 * @param wanted
	 * @param description
	 * @param location
	 *   this  method call the class ;serverproxy, to Contact with the server, 
	  * the main purpose of this method is to update all the details of a chosen item
	  * first we check the Parameters we get (check_input) if something is wrong we return false
	  * than we call the method update_Item that update the chosen item in the data base if something went wrong we return false 
	 * @return return true if everything as ok else return false
	 */
	public boolean updateitem(String  id,String name,String edition,String wanted,String description,String location)
	{
		 ArrayList<String> str_item=new  ArrayList<String>(); 
		 
		 check_input( id,name, edition, wanted, description, location);
		 if(flag==false)
			 return false;
		 
			str_item=BusinessImplements.GetInstance().getServerProxy().update_Item( id,name, edition, wanted, description, location);
			if (str_item.get(0).equals("false"))
			{
				set_msg("\ncoud not update the iten\n please try again "); 
				 return false;
			}
		return flag;
			
	}
	
	/**
	 * 
	 * @param book_id
	 * @param author_id
	 **  * this  method call the class ;serverproxy, to Contact with the server, 
	  * the main purpose of this method is to delete an  author from a chosen item
	  * first we call the method "check_deleteAuthorofItem" that return a few result
	  * if the return result is "cantdelete" we can't delete the author because he is the last author of this item and we return false
	  * if the return result is "inncorectauthorid" the chosen author is not one of the authors of this book therefore we return false
	  * if the return result is "ok" we call the method "deleteAuthorofItem" that delete the author from the item authors list 
	 * @return return true if everything as ok else return false
	 */
	public boolean deleteAuthorofItem(String book_id,String author_id)
	{
		 ArrayList<String> str_check=new  ArrayList<String>(); 
		 ArrayList<String> str_authors=new  ArrayList<String>(); 
		 
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_deleteAuthorofItem(book_id, author_id);
			if (str_check.get(0).equals("cantdelete"))
			{
				set_msg("\nthis item have only this author left\nyou must add unother item before you try to delete this author "); 
				 return false;
			}
			if (str_check.get(0).equals("inncorectauthorid"))
			{
				set_msg("\nthis author is not one of the author of this item\nyou cant delete him "); 
				 return false;
			}
			if(str_check.get(0).equals("ok"))
			{
				str_authors=BusinessImplements.GetInstance().getServerProxy().deleteAuthorofItem(book_id, author_id);
				if (str_authors.get(0).equals("false"))
				{
					set_msg("\ncodnt delete the author please try again ");
					return false;
				}
			}
				return true;
			
	}
	/**
	 * 
	 * @param book_id
	 * @param sub_id
	 * this  method call the class ;serverproxy, to Contact with the server, 
	  * the main purpose of this method is to delete an  subject from a chosen item
	  * first we call the method "check_deleteSubjectofItem" that return a few result
	  * if the return result is "cantdelete" we can't delete the subject because he is the last subject of this item and we return false
	  * if the return result is "inncorectsubjectid" the chosen subject is not one of the subjects of this book therefore we return false
	  * if the return result is "ok" we call the method "deleteSubjectofItem" that delete the subject from the item subjects list 
	 * @return return true if everything as ok else return false
	 */
	public boolean deleteSubjectofItem(String book_id,String sub_id)
	{
		 ArrayList<String> str_check=new  ArrayList<String>(); 
		 ArrayList<String> str_sub=new  ArrayList<String>(); 
		 
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_deleteSubjectofItem(book_id, sub_id);
		
			if (str_check.get(0).equals("cantdelete"))
			{
				set_msg("\nthis item have only this subject left\nyou must add unother item before you try to delete this subject "); 
				 return false;
			}
			if (str_check.get(0).equals("inncorectsubjectid"))
			{
				set_msg("\nthis subject is not one of the subject of this item\nyou cant delete him "); 
				 return false;
			}
			if(str_check.get(0).equals("ok"))
			{
				str_sub=BusinessImplements.GetInstance().getServerProxy().deleteSubjectofItem(book_id, sub_id);
				if (str_sub.get(0).equals("false"))
				{
					set_msg("\ncodnt delete the subject please try again ");
					return false;
				}
			}
				return true;
			
	}
	
	/**
	   * This method checks the parameters we have received are not empty
	  * if they are poses flag= false;
	 * @param id
	 */

	public void check_input(String id)
	{
		//
		if(id.equals(""))
		{
			set_msg("\nmising book id ");
			flag=false;
		}

}
	/**
	 * 
	 * @param id
	 * @param name
	 * @param edition
	 * @param wanted
	 * @param description
	 * @param location
	  * This method checks the parameters we have received are not empty
	  * if they are poses flag= false; 
	 */
	public void  check_input(String  id,String name,String edition,String wanted,String description,String location)
	{
		if(id.equals(""))
		{
			set_msg("\nmising book id ");
			flag=false;
		}
		if(name.equals(""))
		{
			set_msg("\nmising book name ");
			flag=false;
		}
		if(edition.equals(""))
		{
			set_msg("\nmising book edition ");
			flag=false;
		}
		if(wanted.equals(""))
		{
			set_msg("\nmising book wanted ");
			flag=false;
		}
		if(description.equals(""))
		{
			set_msg("\nmising book description ");
			flag=false;
		}
		if(location.equals(""))
		{
			set_msg("\nmising book location ");
			flag=false;
		}
	}
	

}
